package com.java.advance.ticket.redis;

import com.alibaba.fastjson.JSON;
import com.java.advance.ticket.redis.key.KeyPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 提供Redis服务的方法
 * Created by yangminghua on 2018/12/6.
 */
@Component(value = "RedisManager")
public class RedisManager {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 获取单个对象
     *
     * @param keyPrefix
     * @param key
     * @param clz
     * @param <T>
     * @return
     */
    public <T> T get(KeyPrefix keyPrefix, String key, Class<T> clz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPreFix() + "_" + key;
            System.out.println("realKey" + realKey);
            String val = jedis.get(realKey);
            T t = stringToBean(val, clz);
            return t;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 设置对象
     *
     * @param keyPrefix
     * @param key
     * @param value
     * @param <T>
     * @return
     */
    public <T> boolean set(KeyPrefix keyPrefix, String key, T value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPreFix() + "_" + key;
            String val = beanToString(value);
            if (val == null) {
                return false;
            }
            int expiredTime = keyPrefix.expireSeconds();
            if (expiredTime <= 0) {
                jedis.set(realKey, val);
            } else {
                jedis.setex(realKey, expiredTime, val);
            }
            return true;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 判断key是否存在
     *
     * @param keyPrefix
     * @param key
     * @return
     */
    public boolean exists(KeyPrefix keyPrefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPreFix() + "_" + key;
            return jedis.exists(realKey);
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 删除一个缓存
     *
     * @param keyPrefix
     * @param key
     * @return
     */
    public Long delete(KeyPrefix keyPrefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPreFix() + "_" + key;
            Long result = jedis.del(realKey);
            return jedis.del(realKey);
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 增加值
     *
     * @param keyPrefix
     * @param key
     * @return
     */
    public Long incr(KeyPrefix keyPrefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPreFix() + "_" + key;
            return jedis.incr(realKey);
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 减少值
     *
     * @param keyPrefix
     * @param key
     * @return
     */
    public Long decr(KeyPrefix keyPrefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPreFix() + "_" + key;
            return jedis.decr(realKey);
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * bean 转换成String
     *
     * @param value
     * @param <T>
     * @return
     */
    public static <T> String beanToString(T value) {
        if (value == null) {
            return null;
        }
        Class<?> clazz = value.getClass();
        if (clazz == int.class || clazz == Integer.class) {
            return "" + value;
        } else if (clazz == String.class) {
            return (String) value;
        } else if (clazz == long.class || clazz == Long.class) {
            return "" + value;
        } else {
            return JSON.toJSONString(value);
        }
    }

    /**
     * String转换成Bean
     *
     * @param val
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T stringToBean(String val, Class<T> clazz) {
        if (val == null || val.length() <= 0 || clazz == null) {
            return null;
        }
        if (clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(val);
        } else if (clazz == String.class) {
            return (T) String.valueOf(val);
        } else if (clazz == long.class || clazz == Long.class) {
            return (T) Long.valueOf(val);
        } else {
            return JSON.toJavaObject(JSON.parseObject(val), clazz);
        }
    }


    /**
     * 将Redis的连接返回给连接池
     */
    private void returnToPool(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

}
